#!/usr/bin/env ruby

# Based on: http://wiki.github.com/tobi/delayed_job/running-delayedworker-as-a-daemon,
# but modified by Jits, for App4Andy (previously for BioCatalogue) and added by Ian to MethodBox!!.

require 'rubygems'
require 'daemons'

app_dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
 
daemon_options = {
  :multiple => true,
  :dir_mode => :normal,
  :dir => File.join(app_dir, 'tmp', 'pids'),
  :backtrace => true,
  :log_output => true
}
 
Daemons.run_proc('methodbox_delayed_job_worker', daemon_options) do
  if ARGV.include?('--')
    ARGV.slice! 0..ARGV.index('--')
  else
    ARGV.clear
  end
  
  Dir.chdir app_dir
  
  # Should run only in production mode.
  # So if you are testing locally then ensure that your database config
  # has the same database for both development and production modes
  # (though it is not recommend to keep this config at all times!)
  RAILS_ENV = 'production'
  
  puts "Starting in Rails #{RAILS_ENV} mode"
  
  require File.join('config', 'environment')
  
  Delayed::Worker.new.start
end